package com.hopu.mapper;

import com.hopu.entity.Video;
import com.hopu.entity.VideoExt;
import com.hopu.entity.WorkCollectDTO;
import com.hopu.entity.WorkLikeDTO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface VideoMapper {
    @Select("""
             select vi.*,
             (SELECT avatar_url FROM `user`where user.user_id = vi.user_id) userImage,
                       (SELECT username FROM `user`where user.user_id = vi.user_id  ) author,
                       (SELECT COUNT(*) FROM `comment` co where co.video_id = vi.work_id ) commentCount,
                        like_status, collect_status, share_count
                       from video vi left join interaction inter ON vi.work_id = inter.work_id
                       GROUP BY work_id\
            """)
    List<VideoExt> getVideoList();

    /**
     * 查询点赞数
     *
     * @param workId
     * @return
     */
    @Select("SELECT like_count from video where  work_id = #{wordId}")
    Integer getLikeCount(Integer workId);

    /**
     * 查询收藏数
     *
     * @param workId
     * @return
     */
    @Select("SELECT collect_count from video where  work_id = #{wordId}")
    Integer getCollectCount(Integer workId);

    /**
     * 增加点赞数
     *
     * @param workLikeDTO
     */
    @Update("update video.video set like_count = like_count + 1 where work_id =#{workId}")
    void incrementLikeCount(WorkLikeDTO workLikeDTO);

    /**
     * 减少点赞数
     * @param workLikeDTO
     */
    @Update("update video.video set like_count = like_count - 1 where work_id =#{workId}")
    void decrementLikeCount(WorkLikeDTO workLikeDTO);

    /**
     * 增加收藏数
     * @param workCollectDTO
     */
    @Update("update video.video set collect_count=collect_count + 1 where work_id=#{workId}")
    void incrementCollectCount(WorkCollectDTO workCollectDTO);

    /**
     * 减少收藏数
     * @param workCollectDTO
     */
    @Update("update video.video set collect_count=collect_count - 1 where work_id=#{workId}")
    void decrementCollectCount(WorkCollectDTO workCollectDTO);
}
